package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Servlet implementation class PopulateDataBase
 */
public class PopulateDataBase extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PopulateDataBase() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		domain.IContact contact1 = (domain.IContact) context
				.getBean("myContact1");
		domain.IContact contact2 = (domain.IContact) context
				.getBean("myContact2");
		domain.ContactGroup cg1 = (domain.ContactGroup) context
				.getBean("contactGroupTravail");

		domain.IDAOContact daoContact = (domain.IDAOContact) context
				.getBean("DaoContactBean");
		domain.DAOContactGroup daoContactGroup = (domain.DAOContactGroup) context
				.getBean("DaoContactGroupBean");
		try {
			daoContactGroup.addContactGroup(cg1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		daoContact.addContact(contact1);
		daoContact.addContact(contact2);
		RequestDispatcher rd = request.getRequestDispatcher("pages/main.jsp");
		rd.forward(request, response);
	}

}
